// 隐藏手机号的部分数字
exports.hidePartNum = function (val) {
  if (!val) {
    return ''
  }
  return val.replace(/^(\d{3})\d{4}(\d{4})$/, function ($0, $1, $2) {
    return $1 + '****' + $2
  })
}

// 隐藏部分姓名
exports.hidePartName = function (val) {
  if (!val) {
    return ''
  }
  let newValue = val[0]
  if (val.length === 1) {
    newValue = '*'
  }
  for (let i = 1; i < val.length; i++) {
    newValue += '*'
  }
  return newValue
}

// 隐藏身份证的部分数字
exports.hidePartIdCardNum = function (val) {
  if (!val) {
    return ''
  }
  return val.replace(/^(\d{6})\d{8}(\d{4})$/, function ($0, $1, $2) {
    return $1 + '********' + $2
  })
}

// 去除时间的秒
exports.hideSeconds = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3) {
    return $1 + ':' + $2
  })
  return val
}

// 保存两位小数
exports.byteToKB = function (val) {
  return (val / 1024).toFixed(2)
}

// 去除时间
exports.hideTime = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*)\s(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3, $4) {
    return $1
  })
  return val
}

// 'yyyy-MM-dd hh:mm:ss
function formatDate(dateObj, format) {
  var date = {
    'M+': dateObj.getMonth() + 1,
    'd+': dateObj.getDate(),
    'h+': dateObj.getHours(),
    'm+': dateObj.getMinutes(),
    's+': dateObj.getSeconds(),
    'q+': Math.floor((dateObj.getMonth() + 3) / 3),
    'S+': dateObj.getMilliseconds()
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1
        ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return format
}

// 时间戳转为时间 yyyy-MM-dd hh:mm:ss
exports.timestampToDate = function (value, format) {
  format = format || 'yyyy-MM-dd hh:mm:ss'
  var date = new Date()
  date.setTime(value * 1000)
  var dateStr = formatDate(date, format)
  return dateStr
}

// 转义 &lt;
exports.HTMLDecode = function (text) {
  var temp = document.createElement('div')
  temp.innerHTML = text
  var output = temp.innerText || temp.textContent
  temp = null
  return output
}
